package edu.indiana.cs.b534.torrent.impl;

import edu.indiana.cs.b534.torrent.context.TorrentContext;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TestDataCollector {

    public TestDataCollector() {
        Logger log = Logger.getLogger(TorrentManager.TORRENT_MANAGER_NAME);
        log.setLevel(Level.OFF);
    }

    public void runTest(int peerCount, Properties base, int batch) {
        int port = 6570;

        try {
            Properties seedProperties = new Properties();
            seedProperties.putAll(base);

            seedProperties.setProperty("is-seed", "true");
            seedProperties.setProperty("port", String.valueOf(port));
            seedProperties.setProperty("peer-id", "-ERAN-SRINATH-SEED-1");

            TorrentContext seedContext = new TorrentContext(seedProperties);
            if (batch == 0) {
                TorrentManager seed = new TorrentManager(seedContext);
                Thread seedThred = new Thread(seed);
                seedThred.start();
            }
            Thread.sleep(2000);
            TorrentManager[] peers = new TorrentManager[peerCount + batch];
            long startTime = new Date().getTime();
            for (int i = 0; i < peerCount + batch; i++) {
                File f = new File("/tmp/torrents/" + i + "/");
                f.mkdirs();
                f.deleteOnExit();
                Properties peerProperties = new Properties();
                peerProperties.putAll(base);
                peerProperties.setProperty("is-seed", "false");
                peerProperties.setProperty("destination-folder", f.getAbsolutePath());
                peerProperties.setProperty("port", String.valueOf(port + i + 1));
                peerProperties.setProperty("peer-id", "-ERAN-SRINATH-PEER" + batch + i);

                TorrentContext peerContext = new TorrentContext(peerProperties);

                peers[i] = new TorrentManager(peerContext);
                Thread peerThread = new Thread(peers[i]);
                peerThread.start();
                Thread.sleep(2000);

            }
            while (true) {
                boolean completed = true;
                for (TorrentManager peer : peers) {
                    if (!peer.isCompleted()) {
                        completed = false;
                    }
                }
                if (completed) {
                    break;
                }
            }

            long endTime = new Date().getTime();

            System.out.println("Total time to download = " + (endTime-startTime) + "ms");

            for (TorrentManager peer : peers) {
                peer.shutDown();
            }

            System.out.println("Test completed");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            int batch;
            String basePropertiesFile;
            int peerCount = 4;


            if (args.length == 2 || args.length == 3) {
                batch = Integer.parseInt(args[0]);
                basePropertiesFile = args[1];
                if (args.length == 3) {
                    peerCount = Integer.parseInt(args[2]);
                }
                Properties base = new Properties();
                FileInputStream in = new FileInputStream(basePropertiesFile);
                base.load(in);
                in.close();
                TestDataCollector dataCollector = new TestDataCollector();
                dataCollector.runTest(peerCount, base, batch);
            } else {
                System.out.println("batch-no base-prop-file [peer-count]");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("batch-no base-prop-file [peer-count]");
        }

    }

}
